/**
 * 
 */
package com.wbcs.jbsf.auth.interceptor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.jbsf.bean.Role;
import com.wbcs.jbsf.util.Consts;
import com.wbcs.jbsf.util.Wbcs4JBSFUtil;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.intercept.AbsPageInterceptor;

/**
 * 检测session中的角色信息是否存在管理员角色，并将其装换为字符串形式(roleid1,roleid2)
 * @author hawkfly
 */
public class ItprSessionRole extends AbsPageInterceptor
{
    private Log log = LogFactory.getLog(ItprSessionRole.class);
    /* 
     * 将会话中是admin的rolecode转存到request中
     */
    @Override
	public void doStart(ReportRequest rrequest)
    {
        HttpSession session = rrequest.getRequest().getSession();
        Object roleObj = session.getAttribute(Consts.SESSION_ROLE);
        if(roleObj == null){
            log.warn("未登录...");
            return;
        }
        
        @SuppressWarnings("unchecked")
        List<Role> lstRoles = (List<Role>)roleObj;
        List<String> tmplstAdminRoles = new ArrayList<String>();
        List<String> tmplstRoles = new ArrayList<String>();
        for(Role role:lstRoles)
        {
            tmplstRoles.add(role.getRolecode());
            if(role.isAdmin()){
                tmplstAdminRoles.add(role.getRolecode());
            }
        }
        rrequest.getRequest().setAttribute(Consts.REQUEST_ADMINROLES,Wbcs4JBSFUtil.lst2dotStr(tmplstAdminRoles));
        rrequest.getRequest().setAttribute(Consts.REQUEST_ROLECODES,Wbcs4JBSFUtil.lst2dotStr(tmplstRoles));
    }
}

